package com.zishuimuyu.utills;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * TODO
 *
 * @author GJH
 * @date 2023年03月27日
 * @company 北京木联能软件股份有限公司
 */
public class MyFileTypeUtil {

    /**
     * @param sourceFile 源文件
     * @param targetFile 转换后的文件
     * @description 转换普通文本为E文本-天气预报文本
     * @author GJH
     * @date 2023-03-27 027
     */
    public static String transToEFileForWeatherFile(File sourceFile, File targetFile) throws IOException {
        String fileName = "";
        if (FileUtil.isEmpty(sourceFile)) {
            return fileName;
        }
        List<String> lines = FileUtil.readLines(sourceFile, CharsetUtil.CHARSET_UTF_8);
        fileName = sourceFile.getName();
        BufferedWriter fileWriter = FileUtil.getWriter(targetFile, CharsetUtil.CHARSET_UTF_8, false);
        String LINE_SEPARATOR = System.getProperty(SystemUtil.LINE_SEPARATOR);
        StringBuilder builder = StrUtil.builder();
        builder.append("<!SolorWeatherForecasting::MLN!>").append(LINE_SEPARATOR);
        builder.append("<SolorWeatherForecasting>").append(LINE_SEPARATOR);
        builder.append("@ grid	starttime	forecasttime	height	no	windSpeed	windDir	atmos	envTemp	humidity").append(LINE_SEPARATOR);
        for (int i = 0; i < lines.size(); i++) {
            builder.append("# ").append(lines.get(i).replace(" ", "").replace(",", "   ").replace("-", "").replace(":", "")).append(LINE_SEPARATOR);
            // builder.append("#").append(lines.get(i));
        }
        builder.append("</SolorWeatherForecasting>").append(LINE_SEPARATOR);
        fileWriter.write(builder.toString());
        fileWriter.flush();
        fileWriter.close();
        return fileName;
    }

    /**
     * @param sourceFile 源文件
     * @param targetFile 转换后的文件
     * @description 转换普通文本为E文本-功率预测文本
     * @author GJH
     * @date 2023-03-27 027
     */
    public static String transToEFileForPowerFile(File sourceFile, File targetFile) throws IOException {
        String fileName = "";
        if (FileUtil.isEmpty(sourceFile)) {
            return fileName;
        }
        //换行符
        List<String> lines = FileUtil.readLines(sourceFile, CharsetUtil.CHARSET_UTF_8);
        String LINE_SEPARATOR = System.getProperty(SystemUtil.LINE_SEPARATOR);
        fileName = sourceFile.getName();
        BufferedWriter fileWriter = FileUtil.getWriter(targetFile, CharsetUtil.CHARSET_UTF_8, false);

        StringBuilder builder = StrUtil.builder();
        builder.append("<!PowerForecasting::MLN!>").append(LINE_SEPARATOR);
        builder.append("<PowerForecasting>").append(LINE_SEPARATOR);
        builder.append("@ WFarm\tNomPower\tDateTime\tPower").append(LINE_SEPARATOR);
        for (int i = 1; i < lines.size(); i++) {
            builder.append("# ").append(lines.get(i).replace(" ", "").replace(",", "   ").replace("-", "").replace(":", "")).append(LINE_SEPARATOR);
        }
        builder.append("</PowerForecasting>").append(LINE_SEPARATOR);
        fileWriter.write(builder.toString());
        fileWriter.flush();
        fileWriter.close();
        return fileName;
    }

}
